< previous page page_224 next page >

Page 224
In Chapter 9, ''Visual Basic Programming Fundamentals," you learned about writing code to make your computer programs accomplish various tasks. You saw how you can manipulate data and how control statements allow you to execute repetitive tasks and to execute statements selectively. However, creating a good, maintainable program involves more details than just writing code.
One of the tasks you need to be able to perform is to create reusable pieces of code and reusable program pieces so that you are not constantly reinventing the wheel (or the program, in this case). Another important skill is the ability to manage those various pieces of code and forms effectively. This chapter deals with both of these aspects of project management. First, you learn how you can use procedures to eliminate repetitive code in your programs. Then you learn how those procedures and other program components are added to your project. Finally, this chapter gives you a brief look at compiling and distributing your programs for others to use.
Using Procedures and Functions
As you create more and larger programs, you will often find yourself using the same block of code over and over in several places throughout your program and in multiple programs. Surely, there must be a better way to handle repetitive code than to just place it in multiple locations in your program, right? Of course, there is. The solution is to use procedures and functions. Procedures and functions are segments of code that perform a particular task and then return processing to the area of the code from which they were called. This means that a single procedure (or function) can be called from multiple places in your code and, if managed properly, can be used with multiple programs.
You have already been exposed to working with procedures even if you didn't know it. Each time you entered code to be executed by a command button (or other control) in response to an event, you were building an event procedure. Event procedures are called by the program when an event is triggered. As you might already know, you can also create your own procedures and call them when you need them. The procedures that you build are referred to as user-defined sub procedures (or subroutines). Although the code in earlier examples was entirely contained in event procedures, a "real" program might contain more code in user-defined sub procedures.
Working with Procedures
The key idea behind working with procedures is to break down your program into a series of smaller tasks. Each of these tasks can then be encapsulated in a procedure, function, or possibly a class. (Classes are discussed in Chapter 16, "Classes: Reusable Components.") Programming in this manner presents several advantages:
You can test each task individually. The smaller amount of code in a procedure makes debugging easier and makes working on the same project with other developers easier.
You can eliminate redundant code by calling a procedure each time a task needs to be performed instead of repeating the program code.

 
< previous page page_224 next page >